package richbar.com.richbar.adapter.order;

import android.app.Activity;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.common.base.BaseResult;
import com.common.util.ToastUtil;
import com.okhttp.GlideUtil;
import com.okhttp.network.HttpUtils;
import com.okhttp.network.MyGson;
import com.tencent.mm.opensdk.modelpay.PayReq;

import java.util.List;

import richbar.com.richbar.R;
import richbar.com.richbar.activity.order.LookLogisticsAc;
import richbar.com.richbar.activity.product.FirmOrderAc;
import richbar.com.richbar.adapter.shopping.ShoppingAdapter;
import richbar.com.richbar.base.BaseApplication;
import richbar.com.richbar.bean.BaseBean;
import richbar.com.richbar.bean.IsBandBean;
import richbar.com.richbar.bean.OrderResult;
import richbar.com.richbar.bean.OrderSubResult;
import richbar.com.richbar.fragment.order.BaseOrderFragment;
import richbar.com.richbar.help.Constants;
import richbar.com.richbar.util.LoadingDialog;
import richbar.com.richbar.util.NetWorkUtil;

/**
 * Created by QiYang on 2018/12/12.
 */

public class OrderAdapter extends BaseQuickAdapter<OrderResult.DataBean.ListBean> {
    private LoadingDialog loadingDialog;
    public static String Orderid = "";
    public static boolean bln_kind = false;//false 购物车订单 true 个人中心充值
    Activity activity;

    public OrderAdapter(Activity activity, LoadingDialog loadingDialog, int layoutResId, List<OrderResult.DataBean.ListBean> data) {
        super(layoutResId, data);
        this.loadingDialog = loadingDialog;
        this.activity = activity;
    }

    @Override
    protected void convert(BaseViewHolder holder, final OrderResult.DataBean.ListBean bean) {
        holder.setText(R.id.shop_name, bean.getSellerName());
        TextView status1 = holder.getView(R.id.status1);
        TextView status2 = holder.getView(R.id.status2);
        TextView status3 = holder.getView(R.id.status3);
        int status = bean.getStatus();
        int isShip = bean.getIsship();
        int iscomment = bean.getIscomment();
        String goodsStatus = "";
        if (status == 1) {
            //待付款
            goodsStatus = "待付款";
            status1.setVisibility(View.GONE);
            status2.setVisibility(View.GONE);
            status3.setVisibility(View.VISIBLE);
            status3.setText("去支付");
            status3.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Orderid = bean.getId() + "";
                    bln_kind = true;
                    sendPayRequest(bean.getOptions());
                    BaseOrderFragment.sendBraodcast(activity);
                }
            });
        }
        if (status == 2 && isShip == 1) {
            //待发货
            goodsStatus = "待发货";
            status1.setVisibility(View.VISIBLE);
            status2.setVisibility(View.VISIBLE);
            status3.setVisibility(View.GONE);
            status1.setText("取消购买");
            status2.setText("提醒发货");
            status1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    cancle(bean.getId() + "");
                }
            });
            status2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    reminder(bean.getId() + "");
                }
            });
        }
        if (isShip == 2) {
            //待收货
            goodsStatus = "待收货";
            status1.setVisibility(View.GONE);
            status2.setVisibility(View.VISIBLE);
            status3.setVisibility(View.VISIBLE);
            status2.setText("查看物流");
            status3.setText("确认收货");
            status2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    LookLogisticsAc.start(activity,bean.getFmssn());
                }
            });
            status3.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    loadingDialog.show();
                    NetWorkUtil.receiving(bean.getId() + "", new HttpUtils.HttpCallback() {
                        @Override
                        public void onSuccess(String data) {
                            loadingDialog.dismiss();
                            final BaseBean baseBean = MyGson.getInstance().fromJson(data, BaseBean.class);
                            ToastUtil.showToast(activity, baseBean.getMsg());
                            BaseOrderFragment.sendBraodcast(activity);
                        }

                        @Override
                        public void onError(String msg) {
                            super.onError(msg);
                            ToastUtil.showToast(activity, msg);
                        }
                    });
                }
            });
        }
        if (isShip == 3 && status == 2) {
            //售后
            status1.setVisibility(View.GONE);
            status2.setVisibility(View.GONE);
            status3.setVisibility(View.VISIBLE);
            status3.setText("申请售后");
            status3.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ToastUtil.showToast(activity,"暂时未开通");
                }
            });
            goodsStatus = "已完成";
        }
        holder.setText(R.id.status, goodsStatus);
        LinearLayout goods_body = holder.getView(R.id.goods_body);
        RecyclerView recyclerView = holder.getView(R.id.recycleView);
        if (bean.getGoods().size() > 1) {
            goods_body.setVisibility(View.GONE);
            recyclerView.setVisibility(View.VISIBLE);
        } else {
            goods_body.setVisibility(View.VISIBLE);
            recyclerView.setVisibility(View.GONE);
            ImageView pic = holder.getView(R.id.pic);
            if (null != bean.getGoods() && bean.getGoods().size() > 0) {
                GlideUtil.loadImage(mContext, NetWorkUtil.URL + bean.getGoods().get(0).getImg(), pic);
                holder.setText(R.id.goods_name, bean.getGoods().get(0).getName());
            } else {

            }
        }
        holder.setText(R.id.goods_price, "共" + bean.getGoodsNum() + "件商品  需付款：¥" + bean.getAmount());
    }

    private void cancle(String oid) {
        loadingDialog.show();
        NetWorkUtil.cancleOrder(oid, new HttpUtils.HttpCallback() {
            @Override
            public void onSuccess(String data) {
                final BaseResult result = MyGson.getInstance().fromJson(data, OrderResult.class);
                if ("200".equals(result.getCode())){
                    ToastUtil.showToast(activity,"取消购买成功");
                    BaseOrderFragment.sendBraodcast(activity);
                    loadingDialog.dismiss();
                }
            }
        });
    }

    /**
     * 调用微信支付
     */
    public void sendPayRequest(OrderResult.DataBean.ListBean.OptionsBean options) {
        try {
            PayReq req = new PayReq();
            req.appId = Constants.APP_ID;
            req.partnerId = options.getPartnerid();
            req.prepayId = options.getPrepayid();
            req.nonceStr = options.getNoncestr();
            req.timeStamp = options.getTimestamp();
            req.packageValue = options.getPackageX();
            req.sign = options.getSign();
            req.extData = "app data"; // optional
            // 在支付之前，如果应用没有注册到微信，应该先调用IWXMsg.registerApp将应用注册到微信
            BaseApplication.iwxapi.sendReq(req);

        } catch (Exception e) {
            Log.e("PAY_GET", "异常：" + e.getMessage());

        }
    }

    private void reminder(String oid) {
        loadingDialog.show();
        NetWorkUtil.reminderOrder(oid, new HttpUtils.HttpCallback() {
            @Override
            public void onSuccess(String data) {
                final BaseResult result = MyGson.getInstance().fromJson(data, OrderResult.class);
                if ("200".equals(result.getCode())){
                    ToastUtil.showToast(activity,"提醒发货成功");
                    //BaseOrderFragment.sendBraodcast(activity);
                    loadingDialog.dismiss();
                }
            }
        });
    }
}